#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
    FILE* pFile = fopen("./01_fdopen.c", "r");
    if (pFile == NULL)
    {
        perror("fopen");
        exit(-1);
    }
    int fd = fileno(pFile);
    if (fd < 0)
    {
        perror("open");
        exit(-1);
    }

    //read influences fread, fread does not influence read
    char buf[16] = {0};
    read(fd, buf, 4);
    printf("read: %s\n", buf);
    fread(buf, 1, 4, pFile);
    printf("fread: %s\n", buf);
    read(fd, buf, 4);
    printf("read2: %s\n", buf);
    fread(buf, 1, 4, pFile);
    printf("fread2: %s\n", buf);

    //fclose also closes asslociated fd
    fclose(pFile);
    int err = close(fd);    //close: Bad file descriptor
    if (err != 0)
    {
        perror("close");
    }


    return 0;
}